package com.yueya.api.reservation;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.business.reservation.Reservation;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;

import net.sf.json.JSONObject;

public class IReservationService extends BaseService {

	// 新增登记
	public Resp save(JSONObject jo) {

		if (containsKey(jo, "reservname", "tel")) {
			
			String reservname = jo.getString("reservname");
			String tel = jo.getString("tel");
			Reservation reserv = Duang.duang(Reservation.class);
			reserv.set("reservation_name", reservname);
			reserv.set("contact_number", tel);

			// 咨询内容
			if (jo.containsKey("reservtxt")) {
				String reservtxt = jo.getString("reservtxt");
				reserv.set("reservation_txt", reservtxt);
			}

			// 咨询方式
			if (jo.containsKey("reservtype")) {
				String reservtype = jo.getString("reservtype");
				reserv.set("reservation_type", reservtype);
			}

			// 老人姓名
			if (jo.containsKey("seniorname")) {
				String seniorname = jo.getString("seniorname");
				reserv.set("senior_name", seniorname);
			}

			// 老人年龄
			if (jo.containsKey("seniorage")) {
				String seniorage = jo.getString("seniorage");
				reserv.set("senior_age", seniorage);
			}

			// 登记人
			if (jo.containsKey("userid")) {
				String userid = jo.getString("userid");
				reserv.set("registrant_user_id", userid);
			}

			boolean bl = false;
			if (jo.containsKey("reservationid")) {
				reserv.set("reservation_id", jo.getString("reservationid"));
				bl = Reservation.dao.update(reserv);
			}else {
				bl = Reservation.dao.save(reserv);
			}
			 
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	// 获取登记列表
	public Resp getPageList(JSONObject jo) {
		if (containsKey(jo, "pgn", "pgs")) {
			pageNumber = jo.getInt("pgn");
			pageSize = jo.getInt("pgs");
			Map<String, String> param = new HashMap<String, String>();

			// 老人姓名
			if (jo.containsKey("reservname")) {
				String reservname = jo.getString("reservname");
				param.put("reservation_name", reservname);
			}

			// 老人姓名
			if (jo.containsKey("tel")) {
				String tel = jo.getString("tel");
				param.put("contact_number", tel);
			}

			// 咨询日期
			if (jo.containsKey("date")) {
				String date = jo.getString("date");
				param.put("reservation_date", date);
			}

			Page<Record> page = Reservation.dao.paginate(pageNumber, pageSize, param);
			resp.setCode(CodeConst.SUCCESS);
			resp.setData(page);
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

}
